<?php
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("db.inc.php");							//--> db global var
include_once("Smarty.class.php");					//--> out template
include_once("Operation.php");						//--> Operation
include_once("Merchant.php");						//--> Merchant
include_once("ClassCoupon.php");					//--> Coupon
include_once("ClassCategory.php");					//--> category
require_once("controlHeader.php");					//--> system control header
require_once("../tools/fckeditor/fckeditor.php") ;

$objOperate 	= new Operation($objSession->getLanguage());	//--> Operation
$objMerchant	= new Merchant(DB_TAG_COUPON, $uid);			//--> Merchant
$objCoupon 		= new ClassCoupon(DB_TAG_COUPON, $uid);			//--> Coupon
$objCategory 	= new ClassCategory(DB_TAG_COUPON, $uid);		//--> Category

$error_message 	= '';


$isFeatured 		= '0';
$isClearanceSales	= '0';
$isFreeSample		= '0';
$isFreeShipping		= '0';
$isFreeGift			= '0';

$status				= '1';

$startDate			= '';
$startPrice 		= '0.00';
$descript 			= '';



$arrOperate = $objOperate->arrGetFromGroupIDAndMenuID($gid, $menuid);
if(isset($_POST['Submit'])){
	$merchantID			= $_POST['merchantID'];
	$categoryID			= $_POST['categoryID'];
	
	$startDate 			= $_POST['startDate'];
	$expireDate			= $_POST['expireDate'];
	$startPrice 		= $_POST['startPrice'];
	$code 				= addslashes($_POST['code']);
	$amount 			= addslashes($_POST['amount']);
	$url 				= $_POST['url'];
	$longRestr			= addslashes($_POST['longRestr']);

	$comments 			= addslashes($_POST['comments']);
	$descript 			= addslashes($_POST['descript']);
		
	$isFeatured 		= $_POST['isFeatured'];
	$isFreeShipping 	= $_POST['isFreeShipping'];
	$isFreeGift 		= $_POST['isFreeGift'];
	$isClearanceSales 	= $_POST['isClearanceSales'];
	$isFreeSample 		= $_POST['isFreeSample'];
	
	$status 			= $_POST['status'];
	
	if(empty($code) && empty($amount) && empty($descript)){
		$error_message = 'Code or Amount or Descript should\'t be empty.';
	}else{
		$isReturn = $objCoupon->add($arrOperate, $merchantID, $categoryID, $code, $descript, $url, $expireDate, $startDate, $startPrice, $longRestr, 
    		$amount, $isFeatured, $isFreeShipping, $isFreeGift, $isFreeSample, $isClearanceSales, $status, $comments);
		if($isReturn){
			$error_message = 'add successfully.';
			
		}else 
			$error_message = 'add failure.';
	}
}


$arrMerchant	= $objMerchant->listSimple($arrOperate, ' WHERE status = 1 ORDER BY name');
$arrCategories	= $objCategory->listSimple($arrOperate, ' WHERE status = 1 ORDER BY name');



/*----- load FCKEditor -----*/
$oFCKeditor = new FCKeditor('descript') ;

//$oFCKeditor->Height		= 300;
$oFCKeditor->BasePath		= FCKeditor_BASE_PATH;

$oFCKeditor->Value = $descript;

$fckHtml = $oFCKeditor->CreateHtml() ;

$arrStatus 				= array('1' => 'Online', '0' => 'Offline');

$arrIsFeatured 			= array('1' =>'YES', '0' => 'NO');
$arrIsFreeShipping 		= array("1" =>"YES", "0" => "NO");
$arrIsFreeGift 			= array("1" =>"YES", "0" => "NO");
$arrIsFreeSample 		= array("1" =>"YES", "0" => "NO");
$arrIsClearanceSales 	= array("1" =>"YES", "0" => "NO");
 
/*----- out html -----*/
$smarty = new Smarty();		//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = COMPILE_SYS_DIR;
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);

$smarty->assign('error_message', $error_message);
$smarty->assign('backurl',$backurl);

$smarty->assign('arrMerchant', $arrMerchant);
$smarty->assign('merchantID', $merchantID);

$smarty->assign('arrCategories', $arrCategories);
$smarty->assign('categoryID', $categoryID);

$smarty->assign('startDate', $startDate);
$smarty->assign('expireDate', $expireDate);
$smarty->assign('startPrice', $startPrice);
$smarty->assign('code', $code);
$smarty->assign('amount', $amount);
$smarty->assign('url', $url);
$smarty->assign('longRestr', $longRestr);


$smarty->assign('arrIsClearanceSales', $arrIsClearanceSales);
$smarty->assign('isClearanceSales', $isClearanceSales);

$smarty->assign('arrIsFreeShipping', $arrIsFreeShipping);
$smarty->assign('isFreeShipping', $isFreeShipping);

$smarty->assign('arrIsFreeSample', $arrIsFreeSample);
$smarty->assign('isFreeSample', $isFreeSample);

$smarty->assign('arrIsFreeGift', $arrIsFreeGift);
$smarty->assign('isFreeGift', $isFreeGift);

$smarty->assign('arrIsFeatured', $arrIsFeatured);
$smarty->assign('isFeatured', $isFeatured);

$smarty->assign('arrStatus', $arrStatus);
$smarty->assign('status', $status);

$smarty->assign('comments', $comments);

$smarty->assign("fckHtml", $fckHtml);

$smarty->display('listCoupon_Add.htm');
?>
